package com.kaiyidiannao.activity.SecondHandHouse;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.kaiyidiannao.adapter.SecondHandHouseAdapter;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandHouse;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseLIst;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseLIstTop;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseSearch;
import com.kaiyidiannao.view.AutoTextView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by guanzhe on 14-4-15.
 * update by mark on 14-4-21
 */
public class HouseDisplayFragment extends Fragment implements View.OnClickListener, Observer {
    private LinearLayout linearLayout;//根view
    private AutoTextView mTextView02;
    private SecondHandHouseAdapter secondHandHouseAdapter = null;
//    private EditText search;

    TextView secondHandHouseAge,secondHandHouseArea,secondHandHousePrice;

    private ProgressBar progressBar;
    private List<SecondHandHouse> secondHandHouseLists = DataCenter.secondHandHouse;
    private DragListView secondHandHouseList;
    FragmentActivity secondHandHouseActivity = null;

    private View no_content_view;//没有数据的布局对象

    //请求数据参数
    private int catid = DataCenter.secondHandHouse_category_id;
    private int currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private boolean isClose = true;

    int position1;
    int position2;
    int position3;
    String title1,title2,search;
    int page = DataCenter.LISTVIEW_DEFAULT_PAGE;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return linearLayout = (LinearLayout) inflater.inflate(R.layout.house_display, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        catid = DataCenter.secondHandHouse_category_id;
        DataCenter.rolling_current_module_id = DataCenter.moduleFlag.SecondHandHouse;

        secondHandHouseActivity = getActivity();
        findView();

        secondHandHouseList.setVisibility(View.INVISIBLE); // listview隐藏
        progressBar.setVisibility(View.VISIBLE);
        no_content_view.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onResume() {
        super.onResume();
        secondHandHouseActivity = getActivity();
        secondHandHouseArea = (TextView)secondHandHouseActivity.findViewById(R.id.secondHandHouseArea);
        secondHandHousePrice = (TextView)secondHandHouseActivity.findViewById(R.id.secondHandHousePrie);

        if (secondHandHouseLists.size() <= 0) {
            initList();
        }
        if (getArguments() != null) {
            progressBar.setVisibility(View.GONE);
            secondHandHouseList.setVisibility(View.VISIBLE);
            mTextView02.setVisibility(View.VISIBLE);
            if (getArguments().getString("flag").equals("flag1")) {
                position1 = getArguments().getInt("position1");
                title1 = getArguments().getString("list");
                Log.d("flag1", getArguments().getInt("position1") + "");
                secondHandHousePrice.setText(title1);

            }
            if (getArguments().getString("flag").equals("flag2")) {
                position2 = getArguments().getInt("position2");
                title2 = getArguments().getString("list");
                Log.d("flag2", getArguments().getInt("position2") + "");
                secondHandHouseArea.setText(title2);
            }
            if (getArguments().getString("flag").equals("flag3")) {
                search = getArguments().getString("search");
                secondHandHouseArea.setText(search);
            }

            progressBar.setVisibility(View.VISIBLE);
            if(DataCenter.search == null){
                DataCenter.search = "";
            }
            new GetSecondHandHouseSearch(DataCenter.index5,DataCenter.index4,DataCenter.search,page).execute();
//            search.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    progressBar.setVisibility(View.VISIBLE);
//                    new GetSecondHandHouseSearch(DataCenter.index5,DataCenter.index4,DataCenter.index6,page).execute();
//                }
//            });

            // str = getArguments().getString("flag1");
        } else {
            new GetSecondHandHouseLIst(catid).execute();

        }

        if (!HttpUtils.isConnected()) {

        } else {
            new GetSecondHandHouseLIstTop(catid).execute();
        }


        findView();

    }

    //列表为空的时候执行
    private void initList() {
        if (secondHandHouseLists == null || secondHandHouseLists.size() <= 0) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    public void findView() {

        no_content_view = linearLayout.findViewById(R.id.no_content_view);
        secondHandHouseAge = (TextView)secondHandHouseActivity.findViewById(R.id.secondHandHouseAge);
        secondHandHouseList = (DragListView) linearLayout.findViewById(R.id.secondHandHouseList);
        secondHandHouseAdapter = new SecondHandHouseAdapter(secondHandHouseActivity, secondHandHouseLists);
        secondHandHouseList.setAdapter(secondHandHouseAdapter);

//        secondHandHouseAge.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                AlertDialog.Builder dialog = new AlertDialog.Builder(secondHandHouseActivity);
//                LayoutInflater inflater = (LayoutInflater) secondHandHouseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.edittext, null);
//                dialog.setView(layout);
//                search = (EditText)layout.findViewById(R.id.editText);
//                dialog.setPositiveButton("查找", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        String searchC = search.getText().toString();
//                        DataCenter.search = searchC;
//                        progressBar.setVisibility(View.VISIBLE);
//                        secondHandHouseAge.setText(searchC);
//                        new GetSecondHandHouseSearch(DataCenter.index5,DataCenter.index4,searchC,page).execute();
//                    }
//                });
//
//                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                    }
//
//                });
//                dialog.show();
//            }
//        });

        secondHandHouseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(secondHandHouseActivity, SecondHandHouseDetail.class);
                Bundle bundle = new Bundle();
                intent.putExtra("catid", secondHandHouseLists.get(position - 1).getCatid());
                intent.putExtra("itemid", secondHandHouseLists.get(position - 1).getItemid());
                intent.putExtra("itemid", secondHandHouseLists.get(position - 1).getItemid());
                intent.putExtra("catid", secondHandHouseLists.get(position - 1).getCatid());
                intent.putExtra("addtime",secondHandHouseLists.get(position-1).getAddtime());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        secondHandHouseList.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                if (getArguments() != null) {
                    new GetSecondHandHouseSearch(DataCenter.index5,DataCenter.index4,DataCenter.search,page).execute();
                    page = DataCenter.LISTVIEW_DEFAULT_PAGE;
                }else {
                    new GetSecondHandHouseLIst(catid, DataCenter.LISTVIEW_DEFAULT_PAGE).execute();
                    currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
                }
            }

            @Override
            public void onLoadMore() {
                if (getArguments() != null) {
                    new GetSecondHandHouseSearch(DataCenter.index5,DataCenter.index4,DataCenter.search,++page).execute();
                }else {
                    new GetSecondHandHouseLIst(catid, ++currentPage).execute();
                }
            }
        });

        mTextView02 = (AutoTextView) linearLayout.findViewById(R.id.switcher02);
        progressBar = (ProgressBar) linearLayout.findViewById(R.id.second_inner_progress_bar);

    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        DataCenter.isLoad = true;
        if (!HttpUtils.isConnected()) {
            progressBar.setVisibility(View.GONE);
            no_content_view.setVisibility(View.VISIBLE);
            secondHandHouseList.onRefreshComplete();
            secondHandHouseList.onLoadMoreComplete(false);
        }

        if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_LIST_SUCCESS
                || flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_FAILED) { //类表数据
            // secondHandHouseLists.addAll(DataCenter.secondHandHouse);
            progressBar.setVisibility(View.GONE);
            secondHandHouseList.setVisibility(View.VISIBLE);
            secondHandHouseList.onRefreshComplete();
            secondHandHouseList.onLoadMoreComplete(false);
            secondHandHouseAdapter.notifyDataSetChanged();


            if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_LIST_SUCCESS) {

                // Log.d(DataCenter.LOG_INFO, " DdcListActivity.update() -> SUCCESS");
                if ((secondHandHouseLists == null || secondHandHouseLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandHouseList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_empty));
                } else {
                    secondHandHouseList.setVisibility(View.VISIBLE);
                    no_content_view.setVisibility(View.GONE);
                }
            }

            if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_FAILED) {
                if ((secondHandHouseLists == null || secondHandHouseLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandHouseList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_error));
                } else {
                    no_content_view.setVisibility(View.GONE);
                }
            }
        } else { // 置顶滚动栏数据
            ArrayList<SecondHandHouse> topBeanList = DataCenter.secondHandHouseTop;
            mTextView02.setData3(topBeanList);
            mTextView02.startRolling();
        }

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, secondHandHouseActivity + ".onDestroy");
        DataCenter.secondHandHouseTop.clear();
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
